'use strict';
const md5 = require('md5');
const Service = require('egg').Service;

class SysResetService extends Service {
    async changePsw(body) {
        const { app, ctx } = this;
        const { username, old_pass, pass } = body;
        try {
            const user = await ctx.model.Sys.User.findOne({
                where: { username: username },
            });
            if (!user) {
                return { success: false, msg: '账号输入有误！' };
            }
            if (md5(md5(old_pass) + app.config.password_salt) !== user.password) {
                return { success: false, msg: '原密码错误！！' };
            }
            await user.update({
                password: md5(md5(pass) + app.config.password_salt),
                updated_at: ctx.helper.formatTime(new Date()),
                // updated_id: userid,
                upt_act: 'U',
            });
            return { success: true, msg: '修改成功！！' };
        } catch (error) {
            console.log(error);
            return { success: false, msg: '密码修改失败！！' };
        }
    }

}

module.exports = SysResetService;
